package com.sina.shop.manage.service.goods;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.admin.Admin;
import com.sina.shop.domain.goods.GoodsComment;
import com.sina.shop.manage.dao.admin.AdminManageDao;
import com.sina.shop.manage.dao.goods.GoodsCommentManageDao;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-16-14:33
 */
@Service("goodsCommentManageService")
public class GoodsCommentManageServiceImpl extends ShopDataSourceService implements GoodsCommentManageService {

    @Autowired
    @Qualifier("goodsCommentManageDao")
    private GoodsCommentManageDao goodsCommentManageDao;

    @Autowired
    @Qualifier("adminManageDao")
    private AdminManageDao adminManageDao;

    @Override
    public List<Map<String, Object>> getGoodsCommentIdAndName() {
        return null;
    }

    @Override
    public GoodsComment getGoodsCommentById(Long id) {
        return null;
    }

    @Override
    public int deleteGoodsComment(Long id) throws DataException, UpdateException {
        return 0;
    }

    @Override
    public GoodsComment saveGoodsComment(GoodsComment goodsComment) throws CreateException, ResourceExistsException, DataException {
        return null;
    }

    @Override
    public GoodsComment updateGoodsComment(GoodsComment goodsComment) throws UpdateException, DataException {
        return null;
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<Map<String, Object>> getGoodsCommentList(GoodsComment goodsComment, Long[] count) {

        Admin admin = null;
        if (goodsComment.getAdmin() != null && !StringUtils.isEmpty(goodsComment.getAdmin().getAccount())) {
            admin = adminManageDao.getAdmin(goodsComment.getAdmin().getAccount());
            if (admin == null) {
                admin = new Admin();
            }
        }
        goodsComment.setAdmin(admin);

        if (count != null && count.length > 0) {
            count[0] = goodsCommentManageDao.getGoodsCommentCount(goodsComment);
        }
        return goodsCommentManageDao.getGoodsCommentList(goodsComment);
    }

    @Override
    public List<GoodsComment> getGoodsCommentList() {
        return null;
    }
}
